/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.bblocks.common.i18n.demo.util;


import cn.hutool.core.util.StrUtil;
import cn.bblocks.common.i18n.I;
import cn.bblocks.common.i18n.demo.constant.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author Zhao
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private int code;

/*	@Getter
	@Setter
	@Schema(description="响应码 1=成功，其他为失败")
	private Integer result;*/

	@Getter
	@Setter
	private String msg;

/*	@Getter
	@Setter
	@Schema(description="消息/错误消息(code 和 result 一致 && msg 和 message)")
	private String message;*/

	@Getter
	@Setter
	private T data;

	@Getter
	@Setter
	private LocalDateTime time = LocalDateTime.now();



	public static <T> R<T> ok() {
		return restResult(null, CommonConstants.SUCCESS, null);
	}

	public static <T> R<T> ok(T data) {
		return restResult(data, CommonConstants.SUCCESS, null);
	}

	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, CommonConstants.SUCCESS, msg);
	}

	public static <T> R<T> failed() {
		return restResult(null, CommonConstants.FAIL, null);
	}

	public static <T> R<T> failed(String msg) {
		return restResult(null, CommonConstants.FAIL, msg);
	}

	public static <T> R<T> failed(T object) {
		return restResult(object, CommonConstants.FAIL, null);
	}

	/**
	public static <T> R<T> failed(T object, String msg) {
		return restResult(object, CommonConstants.FAIL, msg);
	}
	 **/

	public static <T> R<T> sysError() {
		return  restResult(null,CommonConstants.FAIL, "系统异常");
	}



	public static <T> R<T> failed( int code, String msg) {
		return restResult(null, code, msg);
	}


	public static <T> R<T> restResult(T object, int code, String msg) {
		if(msg == null){
			msg = I.n(String.valueOf(code));
		}

		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setMsg(msg);
		apiResult.setData(object);

		//apiResult.setResult(code);
		//apiResult.setMessage(msg);

		return apiResult;
	}



	private String serviceTips;



	private Boolean sucFlag;

	public String getServiceTips() {
		if(code == CommonConstants.SUCCESS){
			return this.msg;
		}else{
			if(code < 2000 && code!=0){
				return "系统故障,请稍后再试!";
			}else{
				return this.msg;
			}
		}
	}

	/**
	 * 业务错误信息
	 * a、系统错误 目前暂定<2000且不为0 ,跨系统直接提示：“系统故障请稍后再试”
	 * b、业务异常，>=2000以及0 为业务异常，可以直接提示用户
	 * @return
	 */
	public void setServiceTips(String serviceTips) {
	}
	public Boolean getSucFlag() {
		if(code == CommonConstants.SUCCESS){
			return true;
		}else {
			return false;
		}
	}

	public void setSucFlag(Boolean sucFlag) {
	}

}
